package com.lc.zpyh.ui.activity.supermarket;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.lc.zpyh.AdvertiseLinearSmoothScroller;
import com.lc.zpyh.R;
import com.lc.zpyh.adapter.BusinessAdapter;
import com.lc.zpyh.adapter.BusinessColumnAdapter;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.AddCartApi;
import com.lc.zpyh.http.request.AttentioApi;
import com.lc.zpyh.http.request.CancelAttentionApi;
import com.lc.zpyh.http.request.CondimentsQueryApi;
import com.lc.zpyh.http.request.DeleteByIdsApi;
import com.lc.zpyh.http.request.MerchantAnnouncementApi;
import com.lc.zpyh.http.request.MerchantByIDApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SelectCOunpByCanteenIdNewApi;
import com.lc.zpyh.http.request.SelectMerChantEvaluationApi;
import com.lc.zpyh.http.request.SelectMerchantInfoApi;
import com.lc.zpyh.http.request.SelectMyCartApi;
import com.lc.zpyh.http.request.SelectToBeConfirmedApi;
import com.lc.zpyh.http.request.SetByCommodityIdApi;
import com.lc.zpyh.http.request.ShareSelectByTypeApi;
import com.lc.zpyh.http.response.CondimentsQueryBean;
import com.lc.zpyh.http.response.MerchantAnnouncementnBean;
import com.lc.zpyh.http.response.MerchantByIDBean;
import com.lc.zpyh.http.response.SelectCOunpByCanteenIdNewBean;
import com.lc.zpyh.http.response.SelectMerChantEvaluationBean;
import com.lc.zpyh.http.response.SelectMerchantInfonNewBean;
import com.lc.zpyh.http.response.SelectMyCartBean;
import com.lc.zpyh.http.response.SelectToBeConfirmedBean;
import com.lc.zpyh.http.response.SetByCommodityIdBean;
import com.lc.zpyh.http.response.ShareSelectByTypeBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.CourseUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;
import com.lc.zpyh.view.CollectCouponsNewDialog;
import com.lc.zpyh.view.GoodsAttrDialog;
import com.lc.zpyh.view.LooKVideoDialog;
import com.lc.zpyh.view.ShoppingCartDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Title：
 * @Description：
 * @Author：huqinghan(19044311)
 * @Date：2021/3/24
 */
public class BusinessActivity extends AppActivity implements RadioGroup.OnCheckedChangeListener, TextView.OnEditorActionListener {

    private static final String TAG = "BusinessActivity";
    BusinessAdapter adapter;
    BusinessColumnAdapter columnAdapter;
    LinearLayoutManager plvLayoutManager;
    @BindView(R.id.column)
    RecyclerView column;
    @BindView(R.id.plv)
    RecyclerView plv;
    @BindView(R.id.header_title)
    TextView header_title;
    @BindView(R.id.img_dianpu)
    ImageView imgDianpu;
    @BindView(R.id.img_background)
    ImageView imgBackground;

    @BindView(R.id.tv_name)
    TextView tvShopName;
    @BindView(R.id.tv_guanzhu)
    TextView tvGuanzhu;
    @BindView(R.id.btn_guanzhu)
    TextView btnGuanzhu;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.rb_diancan)
    RadioButton rbDiancan;
    @BindView(R.id.rb_pingjia)
    RadioButton rbPingjia;
    @BindView(R.id.rb_shangjia)
    RadioButton rbShangjia;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.rl_diancan)
    RelativeLayout rlDiancan;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.rl_pingjia)
    RelativeLayout rlPingjia;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_yingyeshijian)
    TextView tvYingyeshijian;
    @BindView(R.id.tv_commect)
    TextView tvCommect;
    @BindView(R.id.rl_shangjia)
    RelativeLayout rlShangjia;
    @BindView(R.id.rg_page)
    RadioGroup rgPage;
    @BindView(R.id.rv_yingye)
    RecyclerView rvYingye;
    @BindView(R.id.tv_tag_type1)
    TextView tvTagType1;
    @BindView(R.id.tv_tag_type2)
    TextView tvTagType2;
    @BindView(R.id.tv_xiadan)
    TextView tvXiadan;
    @BindView(R.id.tv_lookvideo)
    TextView tvLookvideo;

    private RadioButton radioButton1, radioButton2;
    private BaseQuickAdapter<SelectMerChantEvaluationBean.ListBean, BaseViewHolder> pingjiaadapter;
    List<SelectMyCartBean.ListBean.CartsBean> dataBeanList = new ArrayList<>();
    List<String> orderList = new ArrayList<>();
    List<CondimentsQueryBean.ListBean> dataxiaoliaoList = new ArrayList<>();
    private ShoppingCartDialog shoppingCartDialog;
    private String merchantid;
    private String phone;
    //    店铺关注状态
    private int userAttention;
    private GoodsAttrDialog goodsAttrDialog;

    private List<Object> mList;
    private String video;
    private CollectCouponsNewDialog collectCouponsDialog;
    private String shopname = "";
    private int attentioncount;
    private SelectMerchantInfonNewBean data;
    private String title;
    private String description;
    private String logo;
    private String url;

    private void getData() {
        mList.clear();
//        mList.addAll()
    }

    List<String> DangAnList = new ArrayList<>();
    private BaseQuickAdapter<MerchantByIDBean.ListBean.MerchantTimeBean, BaseViewHolder> yingyeadapter;

    private void getNext() {
        mList.addAll(mList);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business;
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        rgPage.setOnCheckedChangeListener(this);
        merchantid = getIntent().getStringExtra("id");
        radioButton1 = findViewById(R.id.rb_diancan);
        radioButton2 = findViewById(R.id.rb_pingjia);
        radioButton1.setChecked(true);
        shareSelectByType();
        etSearch.setOnEditorActionListener(this);
        plvLayoutManager = new LinearLayoutManager(BusinessActivity.this) {
            //
            @Override
            public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
                AdvertiseLinearSmoothScroller linearSmoothScroller =
                        new AdvertiseLinearSmoothScroller(recyclerView.getContext());
                linearSmoothScroller.setTargetPosition(position);
                startSmoothScroll(linearSmoothScroller);
            }
        };
        plvLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        plv.setLayoutManager(plvLayoutManager);
        adapter = new BusinessAdapter(BusinessActivity.this);
        adapter.setOnGoodsOrderListener(new BusinessAdapter.OnAddCartrListener() {
            @Override
            public void onAddShoppingCart(String Id, String count, String prePrice, String price, String commodityName, String condimentsId, String cartId, String countcart) {
// 点击加号 判断是否有规格
                isAttr(Id, count, prePrice, price, commodityName, condimentsId, cartId, countcart);

            }

            @Override
            public void onReduceShoppingCart(String Id, String count, String prePrice, String price, String commodityName, String condimentsId, String specificationId, String countcart) {
                Log.e(TAG, "onReduceShoppingCart: " + countcart);

                addShopCart(Id, count, specificationId, price, condimentsId);
            }
        });
        plv.setAdapter(adapter);


        LinearLayoutManager columnLayoutManager = new LinearLayoutManager(BusinessActivity.this);
        columnLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        column.setLayoutManager(columnLayoutManager);
        columnAdapter = new BusinessColumnAdapter(BusinessActivity.this, new BusinessColumnAdapter.CallBack() {
            @Override
            public void onClick(int index) {
                plv.smoothScrollToPosition(adapter.getItemIndexForGroup(index));
            }
        });
        column.setAdapter(columnAdapter);


        plv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                try {


                    int firstVisibleItem = plvLayoutManager.findFirstVisibleItemPosition();
                    Log.e("-----onScrolled-----", "firstVisibleItem: " + firstVisibleItem);
                    if (firstVisibleItem >= 0) {
                        header_title.setVisibility(View.VISIBLE);
                        Log.e(TAG, "onScrolled: " + adapter.getItem(firstVisibleItem).optString("classifyName"));
                        header_title.setText(adapter.getItem(firstVisibleItem).optString("classifyName"));
                        if (firstVisibleItem == 0 || adapter.isChildGroup(firstVisibleItem)) {
                            columnAdapter.setCurrentIndex(adapter.getGroupIndex(firstVisibleItem));
                        }
                        Log.e("----onScrolled--------", "ChildCount: " + recyclerView.getChildCount());
                        if (adapter.isGroup(firstVisibleItem + 1)) {
                            View childView = plvLayoutManager.findViewByPosition(firstVisibleItem);

                            if (childView != null) {
                                int titleHeight = header_title.getHeight();
                                int bottom = childView.getBottom();
                                Log.e("-----onScrolled-------", titleHeight + "   " + bottom);
                                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) header_title.getLayoutParams();
                                if (bottom <= titleHeight) {
                                    float pushedDistance = bottom - titleHeight;
                                    params.topMargin = (int) pushedDistance;
                                    header_title.setLayoutParams(params);
                                } else {
                                    if (params.topMargin != 0) {
                                        params.topMargin = 0;
                                        header_title.setLayoutParams(params);
                                    }
                                }
                            }
                        } else {
                            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) header_title.getLayoutParams();
//                        if (params.topMargin != 0) {
                            params.topMargin = 0;
                            header_title.setLayoutParams(params);
//                        }
                        }

                    } else {
                        header_title.setVisibility(View.INVISIBLE);
                    }
                } catch (NullPointerException e) {

                }
            }
        });

//        评价
        pingjia();
//        商家
        shangjia();
//        shangjia();
//        公告
        shangjiagonggao();


    }

    @Override
    protected void onStart() {
        super.onStart();
        initSelectMerchantInfoStart();


    }

    /**
     * 商家公告
     */
    private void shangjiagonggao() {
        EasyHttp.post(this)
                .api(new MerchantAnnouncementApi().setMerchantid(merchantid)).request(new HttpCallback<MerchantAnnouncementnBean>(this) {

            @Override
            public void onSucceed(MerchantAnnouncementnBean data) {
                if (data.getCode() == 0) {
                    tvCommect.setText(data.getList());
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    /**
     * 商家信息
     */
    private void shangjia() {
        Log.e("TAG", "shangjia: " + SPUtil.get(BusinessActivity.this, IntentKey.TOKEN, ""));
        EasyHttp.post(this)
                .api(new MerchantByIDApi().setToken(SPUtil.get(BusinessActivity.this, IntentKey.TOKEN, "")).setMerchantid(merchantid)).request(new HttpCallback<MerchantByIDBean>(this) {

            @Override
            public void onSucceed(MerchantByIDBean data) {
                if (data.getCode() == 0) {
                    tvAddress.setText(data.getList().getSchoolName() + data.getList().getCanteenName());


                    phone = data.getList().getIphone();
                    if (CourseUtils.isNotNullOrEmpty(data.getList().getQualificationImg1())) {
                        DangAnList.add(data.getList().getQualificationImg1());
                    }
                    if (CourseUtils.isNotNullOrEmpty(data.getList().getQualificationImg2())) {
                        DangAnList.add(data.getList().getQualificationImg2());
                    }
                    if (CourseUtils.isNotNullOrEmpty(data.getList().getQualificationImg3())) {
                        DangAnList.add(data.getList().getQualificationImg3());
                    }
                    if (CourseUtils.isNotNullOrEmpty(data.getList().getQualificationImg4())) {
                        DangAnList.add(data.getList().getQualificationImg4());
                    }
                    yingyeadapter = new BaseQuickAdapter<MerchantByIDBean.ListBean.MerchantTimeBean, BaseViewHolder>
                            (R.layout.item_yingye, data.getList().getMerchantTime()) {

                        @Override
                        protected void convert(BaseViewHolder baseViewHolder, MerchantByIDBean.ListBean.MerchantTimeBean dataBean) {
                            baseViewHolder.setText(R.id.tv_yingyeshijian, dataBean.getStartTime() + "-" + dataBean.getEndTime());

                        }
                    };
                    rvYingye.setAdapter(yingyeadapter);
                }

            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }

    /**
     * 商家是否有小料
     */
    private void shangjiaxiaoliao(String name1, List<SetByCommodityIdBean.ListBean.SpecificationCategoryListOneBean> listone, String name2, List<SetByCommodityIdBean.ListBean.SpecificationCategoryListTwoBean> listtwo, String id, String commodityName, String count, String price, String condimentsId, String cartId, String countcart) {
        EasyHttp.post(this)
                .api(new CondimentsQueryApi().setCommodityId(id)).request(new HttpCallback<CondimentsQueryBean>(this) {

            @Override
            public void onSucceed(CondimentsQueryBean data) {
                if (data.getCode() == 0) {
                    dataxiaoliaoList.clear();
                    dataxiaoliaoList.addAll(data.getList());
                    if (listone != null || name1 != null || listtwo != null || data.getList().size() > 0) {
                        goodsAttrDialog = new GoodsAttrDialog(BusinessActivity.this, name1, listone, name2, listtwo, dataxiaoliaoList, id, commodityName, count, price, cartId, countcart);
                        goodsAttrDialog.show();
                        goodsAttrDialog.setOnGoodsAttrListener(new GoodsAttrDialog.OnGoodsAttrListener() {

                            @Override
                            public void onAddShoppingCart(String id, String count, String specificationId, String price, String condimentsId, String cartId, String countcart) {
//                                adapter.setCountData(cartId,countcart);
                                addShopCartAttr(id, count, specificationId, price, condimentsId);
                            }

                            @Override
                            public void dissmiss() {
//                                selectMyCart();
//                                initSelectMerchantInfo();
                            }


                        });
                    } else {
//                        if (count.equals("0")) {
//                            deleteByIds(id);
//                        } else {

                        addShopCart(id, count, "", price, condimentsId);
//                        }
                    }
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void addShopCartAttr(String id, String count, String specificationId, String price, String condimentsId) {
        EasyHttp.post(this)
                .api(new AddCartApi().setUserId(SPUtil.get(this, IntentKey.USERID, "")).setCount(count).setCommodityId(id).setMerchantId(merchantid).setSpecificationId(specificationId).setPrice(price).setCondimentsId(condimentsId)).request(new OnHttpListener<PublicMsgBean>() {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    selectMyCart();
//
                } else {
                    toast(data.getList());
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    /**
     * 商家评价
     */
    private void pingjia() {
        EasyHttp.post(this)
                .api(new SelectMerChantEvaluationApi().setMerchantid(merchantid)).request(new HttpCallback<SelectMerChantEvaluationBean>(this) {

            @Override
            public void onSucceed(SelectMerChantEvaluationBean data) {
                if (data.getCode() == 0) {

                    pingjiaadapter = new BaseQuickAdapter<SelectMerChantEvaluationBean.ListBean, BaseViewHolder>
                            (R.layout.item_busimess_myevaluate, data.getList()) {

                        @Override
                        protected void convert(BaseViewHolder viewHolder, SelectMerChantEvaluationBean.ListBean commentOrderBean) {

                            viewHolder.setText(R.id.tv_shop_name, commentOrderBean.getUserName());
                            viewHolder.setText(R.id.tv_busimess, commentOrderBean.getCommodityName());
                            viewHolder.setText(R.id.tv_date, commentOrderBean.getCommentTime());
                            if (commentOrderBean.getNameOne() == null) {
                                viewHolder.setText(R.id.tv_specs, "规格:默认");
                            } else {
                                if (commentOrderBean.getNameTwo() == null) {
                                    viewHolder.setText(R.id.tv_specs, "规格:" + commentOrderBean.getNameOne());
                                } else {
                                    viewHolder.setText(R.id.tv_specs, "规格:" + commentOrderBean.getNameOne() + "、" + commentOrderBean.getNameTwo());

                                }
                            }

                            GlideApp.with(getContext()).load(IntentKey.CAR_IMAGE + commentOrderBean.getUserImg()).into(((ImageView) viewHolder.getView(R.id.img_head)));
                            int starSpeed = commentOrderBean.getDeliverySpeed();
                            viewHolder.setImageResource(R.id.iv_speed_start_1, starSpeed >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_speed_start_2, starSpeed >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_speed_start_3, starSpeed >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_speed_start_4, starSpeed >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_speed_start_5, starSpeed >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);

                            int starhealth = commentOrderBean.getHealthSituation();
                            viewHolder.setImageResource(R.id.iv_hygiene_start_1, starhealth >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_hygiene_start_2, starhealth >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_hygiene_start_3, starhealth >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_hygiene_start_4, starhealth >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_hygiene_start_5, starhealth >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            int starpreservation = commentOrderBean.getHeatPreservation();
                            viewHolder.setImageResource(R.id.iv_effect_start_1, starpreservation >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_effect_start_2, starpreservation >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_effect_start_3, starpreservation >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_effect_start_4, starpreservation >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                            viewHolder.setImageResource(R.id.iv_effect_start_5, starpreservation >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                        }
                    };

                    View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);

                    pingjiaadapter.setEmptyView(emptyview);
                    recyclerView.setAdapter(pingjiaadapter);
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });


    }

    /**
     * 关注商家
     */
    private void attention() {
        EasyHttp.post(this)
                .api(new AttentioApi().setUserId(SPUtil.get(this, IntentKey.USERID, "")).setMerchantid(merchantid)).request(new HttpCallback<SelectMerChantEvaluationBean>(this) {

            @Override
            public void onSucceed(SelectMerChantEvaluationBean data) {
                if (data.getCode() == 0) {
                    btnGuanzhu.setText("已关注");
                    userAttention = 1;
                    tvGuanzhu.setText((attentioncount + 1) + "");
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    /**
     * 取消关注
     */
    private void cancelAttention() {
        EasyHttp.post(this)
                .api(new CancelAttentionApi().setUserId(SPUtil.get(this, IntentKey.USERID, "")).setMerchantid(merchantid)).request(new HttpCallback<SelectMerChantEvaluationBean>(this) {

            @Override
            public void onSucceed(SelectMerChantEvaluationBean data) {
                if (data.getCode() == 0) {
                    userAttention = 0;
                    btnGuanzhu.setText("关注");
                    tvGuanzhu.setText((attentioncount) + "");
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });


    }

    @OnClick({R.id.image_back, R.id.btn_guanzhu, R.id.tv_baozhang, R.id.img_dianhua, R.id.rl_bottom, R.id.tv_xiadan, R.id.tv_lookvideo, R.id.tv_tag_type2, R.id.tv_tag_type1, R.id.title_bar_txt_right, R.id.rl_fenxiang})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.rl_fenxiang:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    getSystemService(ClipboardManager.class).setPrimaryClip(ClipData.newPlainText("url", description));
                }
                ToastUtils.show(R.string.share_platform_copy_hint);
//                new ShareDialog.Builder(this)
//                        // 分享标题
//                        .setShareTitle(title)
//                        // 分享描述
//                        .setShareDescription(description)
//                        // 分享缩略图
//                        .setShareLogo(IntentKey.CAR_IMAGE + logo)
//                        // 分享链接
//                        .setShareUrl(url)
//                        .setListener(new UmengShare.OnShareListener() {
//
//                            @Override
//                            public void onSucceed(Platform platform) {
//                                toast("分享成功");
//                            }
//
//                            @Override
//                            public void onError(Platform platform, Throwable t) {
//                                toast("分享出错");
//                            }
//
//                            @Override
//                            public void onCancel(Platform platform) {
//                                toast("分享取消");
//                            }
//                        })
//                        .show();
                break;
            case R.id.title_bar_txt_right:
                if (etSearch.getText().toString().trim().isEmpty()) {
                    toast("请输入搜索信息");
                }
                startActivity(new Intent(this, BusinessSearchActivity.class).putExtra("merchantid", merchantid).putExtra("shopname", shopname).putExtra("searchname", etSearch.getText().toString().trim()));
                break;
            case R.id.tv_lookvideo:
                if (video == null || video.isEmpty() || video.equals(IntentKey.CAR_IMAGE)) {
                    toast("暂无可播放视频");
                    return;
                }
                LooKVideoDialog LooKVideoDialog = new LooKVideoDialog(this, video, tvShopName.getText().toString());
                LooKVideoDialog.show();
                break;
            case R.id.tv_xiadan:
//                下单
                selectToBeConfirmed();

                break;
            case R.id.rl_bottom:
                shoppingCartDialog = new ShoppingCartDialog(BusinessActivity.this, merchantid, dataBeanList);
                shoppingCartDialog.show();
                shoppingCartDialog.setOnGoodsOrderListener(new ShoppingCartDialog.OnGoodsOrderListener() {
                    @Override
                    public void onDismiss(String[] specification, String num, String shopPrice, String intPrice, String vipPrice, String integralPrice, String deductionIntegral) {

                    }

                    @Override
                    public void onAddShoppingCart(String id, String count, String prePrice, String price, String condimentsId, String cardId, String cartcount) {

                        addShopCartShop(id, count, prePrice, price, condimentsId);
//
                    }

                    @Override
                    public void onSubShoppingCart() {
                        selectMySubCart();
//
                    }

                    @Override
                    public void OrderCom(List<String> orderList) {
                        selectToBeConfirmed();
                    }


                });

                break;
            case R.id.btn_guanzhu:
                if (userAttention == 0) {
                    attention();

                } else {
                    cancelAttention();
                }
                break;
            case R.id.tv_baozhang:
                startActivity(new Intent(this, DangAnActivity.class).putExtra("list", (Serializable) DangAnList));


                break;
            case R.id.img_dianhua:
                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));//跳转到拨号界面，同时传递电话号码
                startActivity(dialIntent);
                break;
            case R.id.tv_tag_type1:
                coupon(shopname);


                break;
            case R.id.tv_tag_type2:
                coupon(shopname);
                break;
        }
    }

    private void selectMySubCart() {
        EasyHttp.post(this)
                .api(new SelectMyCartApi().setUserid(SPUtil.get(this, IntentKey.USERID, "")).setPageNum("1").setPageSize("99").setMerchantid(merchantid).setSchoolid(SPUtil.get(this, IntentKey.ADDRESSID, ""))).request(new OnHttpListener<SelectMyCartBean>() {

            @Override
            public void onSucceed(SelectMyCartBean cartdata) {
                if (cartdata.getCode() == 0) {
                    if (cartdata.getList().size() > 0) {
                        spcunchu(cartdata, cartdata.getList().size());

                        tvXiadan.setVisibility(View.VISIBLE);
                        String totalNum = "0";
                        String totalPrice = "0";
                        for (int i = 0; i < cartdata.getList().get(0).getCarts().size(); i++) {
                            if (cartdata.getList().get(0).getCarts().get(i).getSpzt() == 0 && cartdata.getList().get(0).getCarts().get(i).getGgzzt() == 0) {
                                totalNum = BigDecimalUtils.add(totalNum, cartdata.getList().get(0).getCarts().get(i).getCount() + "", 0);
                                if (cartdata.getList().get(0).getCarts().get(i).getGgzjg() != null) {
                                    totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getGgzjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                } else {
                                    if (cartdata.getList().get(0).getCarts().get(i).getPrice() != null) {
                                        totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getPrice() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                    } else {

                                        if (cartdata.getList().get(0).getCarts().get(i).getSpjg() != null) {
                                            totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getSpjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                        } else {
                                            totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getSpyjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                        }
                                    }
                                }

                            }
                        }
                        tvMoney.setText(totalPrice);
                        tvNum.setText("已选" + totalNum + "件");
                        dataBeanList.clear();
                        if (cartdata.getList().size() > 0) {
                            dataBeanList.addAll(cartdata.getList().get(0).getCarts());
                        }
                        if (shoppingCartDialog != null) {
                            shoppingCartDialog.setDataList(dataBeanList);
                        }
                    } else {
                        initSelectMerchantInfoStart();
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void deleteByIds(String id) {
        EasyHttp.post(this)
                .api(new DeleteByIdsApi().setIds(id)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {

                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void addShopCartShop(String id, String count, String specificationId, String price, String condimentsId) {

        EasyHttp.post(this)
                .api(new AddCartApi().setUserId(SPUtil.get(this, IntentKey.USERID, "")).setCount(count).setCommodityId(id).setMerchantId(merchantid).setSpecificationId(specificationId).setPrice(price).setCondimentsId(condimentsId)).request(new OnHttpListener<PublicMsgBean>() {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    selectMyCart();

//                    initSelectMerchantInfo();
                } else {
//                    initSelectMerchantInfo();
                    toast(data.getList());
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void selectToBeConfirmed() {
        orderList.clear();
        if (dataBeanList.size() != 0) {
            for (int i = 0; i < dataBeanList.size(); i++) {
                orderList.add(dataBeanList.get(i).getCartId() + "");
            }
        } else {
            toast("购物车还空着");
            return;
        }
        EasyHttp.post(this)
                .api(new SelectToBeConfirmedApi().setUserid(SPUtil.get(this, IntentKey.USERID, "")).setSchoolId(SPUtil.get(this, IntentKey.ADDRESSID, "")).setCartIds(CourseUtils.listToString1(orderList))).request(new HttpCallback<SelectToBeConfirmedBean>(this) {

            @Override
            public void onSucceed(SelectToBeConfirmedBean data) {
                if (data.getCode() == 0) {
                    startActivity(new Intent(BusinessActivity.this, ConfirmOrderActivity.class).putExtra("orderList", data.getList()).putExtra("cartIds", CourseUtils.listToString1(orderList)));
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }


    //    用户点击进入商家,查询商家信息
    private void initSelectMerchantInfoStart() {


        EasyHttp.post(this)
                .api(new SelectMerchantInfoApi().setUserid(SPUtil.get(this, IntentKey.USERID, "")).setMerchantid(merchantid)).request(new HttpCallback<String>(this) {

            @Override
            public void onSucceed(String s) {
//                把数据转成StingSP存储在本地
                SPUtil.remove(BusinessActivity.this, IntentKey.TEXT);
                SPUtil.put(BusinessActivity.this, IntentKey.TEXT, s);
                String ss = SPUtil.get(BusinessActivity.this, IntentKey.TEXT, "");
                Gson gson = new Gson();
//                将存储的数据转成实体类
                data = gson.fromJson(s, new TypeToken<SelectMerchantInfonNewBean>() {
                }.getType());
                Log.e(TAG, "onSucceed: " + JSON.toJSONString(data));
                Log.e("SPManager", "onSucceed:222222222 " + s.toString());
                selectMyCart();
                if (data.getCode() == 0) {

                    if (shopname.isEmpty()) {
                        coupon(data.getMerchantInfo().getName());
                    }

                    Log.e("SPManager", "onSucceed:222222222 " + data.toString());

                    shopname = data.getMerchantInfo().getName();
                    tvShopName.setText(data.getMerchantInfo().getName());
                    attentioncount = data.getMerchantInfo().getAttentionCount();
                    tvGuanzhu.setText(data.getMerchantInfo().getAttentionCount() + "");
                    if (data.getMerchantInfo().getStatus() == 1) {
                        tvXiadan.setText("商家打样");
                        tvXiadan.setEnabled(false);
                    } else {
                        tvXiadan.setText("下单");
                        tvXiadan.setEnabled(true);
                    }
                    if (data.getMerchantInfo().getVideo() != null) {
                        if (data.getMerchantInfo().getVideo().contains("http")) {
                            video = data.getMerchantInfo().getVideo();
                        } else {
                            video = IntentKey.CAR_IMAGE + data.getMerchantInfo().getVideo();
                        }
                        tvLookvideo.setVisibility(View.VISIBLE);
                    }

                    if (data.getMerchantInfo().getImg() != null) {
                        if (data.getMerchantInfo().getImg().contains("http")) {
                            GlideApp.with(BusinessActivity.this)
                                    .load(data.getMerchantInfo().getImg())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgDianpu);
                        } else {
                            GlideApp.with(BusinessActivity.this)
                                    .load(IntentKey.CAR_IMAGE + data.getMerchantInfo().getImg())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgDianpu);
                        }
                    }
                    if (data.getMerchantInfo().getBackdrop() != null) {
                        if (data.getMerchantInfo().getBackdrop().contains("http")) {
                            GlideApp.with(BusinessActivity.this)
                                    .load(data.getMerchantInfo().getBackdrop())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgBackground);
                        } else {
                            GlideApp.with(BusinessActivity.this)
                                    .load(IntentKey.CAR_IMAGE + data.getMerchantInfo().getBackdrop())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgBackground);
                        }

                    }
                    radioButton2.setText("评价(" + data.getMerchantInfo().getEvaluationCount() + ")");
                    userAttention = data.getMerchantInfo().getUserAttention();
                    if (data.getMerchantInfo() != null) {
                        if (data.getMerchantInfo().getUserAttention() == 0) {
                            btnGuanzhu.setText("关注");
                        } else {
                            btnGuanzhu.setText("已关注");
                        }
                        if (data.getMerchantInfo() != null) {
                            if (data.getMerchantInfo().getClassifies() != null && data.getMerchantInfo().getClassifies().size() > 0) {
                                if (data.getMerchantInfo().getMerchantCoupons().size() > 0 && data.getMerchantInfo().getMerchantCoupons().size() < 2) {

                                    tvTagType1.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getCreditFor() + "", "100", 0));
                                    tvTagType1.setVisibility(View.VISIBLE);
                                    tvTagType2.setVisibility(View.INVISIBLE);

                                } else if (data.getMerchantInfo().getMerchantCoupons().size() >= 2) {
                                    tvTagType1.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getCreditFor() + "", "100", 0));
                                    tvTagType2.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(1).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(1).getCreditFor() + "", "100", 0));
                                    tvTagType1.setVisibility(View.VISIBLE);
                                    tvTagType2.setVisibility(View.VISIBLE);

                                } else {
                                    tvTagType1.setVisibility(View.INVISIBLE);
                                    tvTagType2.setVisibility(View.INVISIBLE);
                                }
                                if (data.getMerchantInfo().getClassifies().get(0).getChildrens() != null) {
                                    JSONObject jo = null;
                                    try {
                                        jo = new JSONObject(com.alibaba.fastjson.JSONObject.toJSONString(data.getMerchantInfo()));

                                        ;
                                        initColumnData(jo);


                                        adapter.setData(initDataa(jo), merchantid, tvShopName.getText().toString(), null, null);

                                        columnAdapter.setData(initColumnData(jo));

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }


                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);
            }
        });
    }

    //    用户点击进入商家,查询商家信息
    private void initSelectMerchantInfo() {
        EasyHttp.post(this)
                .api(new SelectMerchantInfoApi().setUserid(SPUtil.get(this, IntentKey.USERID, "")).setMerchantid(merchantid)).request(new OnHttpListener<SelectMerchantInfonNewBean>() {

            @Override
            public void onSucceed(SelectMerchantInfonNewBean data) {
                if (data.getCode() == 0) {


                    if (shopname.isEmpty()) {
                        coupon(data.getMerchantInfo().getName());
                    }
                    shopname = data.getMerchantInfo().getName();
                    tvShopName.setText(data.getMerchantInfo().getName());
                    attentioncount = data.getMerchantInfo().getAttentionCount();
                    tvGuanzhu.setText(data.getMerchantInfo().getAttentionCount() + "");
                    if (data.getMerchantInfo().getStatus() == 1) {
                        tvXiadan.setText("商家打样");
                        tvXiadan.setEnabled(false);
                    } else {
                        tvXiadan.setText("下单");
                        tvXiadan.setEnabled(true);
                    }
                    if (data.getMerchantInfo().getVideo() != null) {
                        if (data.getMerchantInfo().getVideo().contains("http")) {
                            video = data.getMerchantInfo().getVideo();
                        } else {
                            video = IntentKey.CAR_IMAGE + data.getMerchantInfo().getVideo();
                        }
                    }

                    if (data.getMerchantInfo().getImg() != null) {
                        if (data.getMerchantInfo().getImg().contains("http")) {
                            GlideApp.with(BusinessActivity.this)
                                    .load(data.getMerchantInfo().getImg())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgDianpu);
                        } else {
                            GlideApp.with(BusinessActivity.this)
                                    .load(IntentKey.CAR_IMAGE + data.getMerchantInfo().getImg())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgDianpu);
                        }
                    }
                    if (data.getMerchantInfo().getBackdrop() != null) {
                        if (data.getMerchantInfo().getBackdrop().contains("http")) {
                            GlideApp.with(BusinessActivity.this)
                                    .load(data.getMerchantInfo().getBackdrop())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgBackground);
                        } else {
                            GlideApp.with(BusinessActivity.this)
                                    .load(IntentKey.CAR_IMAGE + data.getMerchantInfo().getBackdrop())
                                    .placeholder(R.mipmap.zhanweiicon01)
                                    .error(R.mipmap.zhanweiicon01)

                                    .into(imgBackground);
                        }

                    }
                    radioButton2.setText("评价(" + data.getMerchantInfo().getEvaluationCount() + ")");
                    userAttention = data.getMerchantInfo().getUserAttention();
                    if (data.getMerchantInfo() != null) {
                        if (data.getMerchantInfo().getUserAttention() == 0) {
                            btnGuanzhu.setText("关注");
                        } else {
                            btnGuanzhu.setText("已关注");
                        }
                        if (data.getMerchantInfo() != null) {
                            if (data.getMerchantInfo().getClassifies() != null && data.getMerchantInfo().getClassifies().size() > 0) {
                                if (data.getMerchantInfo().getMerchantCoupons().size() > 0 && data.getMerchantInfo().getMerchantCoupons().size() < 2) {

                                    tvTagType1.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getCreditFor() + "", "100", 0));
                                    tvTagType1.setVisibility(View.VISIBLE);
                                    tvTagType2.setVisibility(View.INVISIBLE);

                                } else if (data.getMerchantInfo().getMerchantCoupons().size() >= 2) {
                                    tvTagType1.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(0).getCreditFor() + "", "100", 0));
                                    tvTagType2.setText("满" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(1).getMeetTheConditions() + "", "100", 0) + "减" + BigDecimalUtils.div(data.getMerchantInfo().getMerchantCoupons().get(1).getCreditFor() + "", "100", 0));
                                    tvTagType1.setVisibility(View.VISIBLE);
                                    tvTagType2.setVisibility(View.VISIBLE);

                                } else {
                                    tvTagType1.setVisibility(View.INVISIBLE);
                                    tvTagType2.setVisibility(View.INVISIBLE);
                                }
                                if (data.getMerchantInfo().getClassifies().get(0).getChildrens() != null) {
                                    JSONObject jo = null;
                                    try {
                                        jo = new JSONObject(com.alibaba.fastjson.JSONObject.toJSONString(data.getMerchantInfo()));

                                        ;
                                        initColumnData(jo);


                                        adapter.setData(initDataa(jo), merchantid, tvShopName.getText().toString(), null, null);

                                        columnAdapter.setData(initColumnData(jo));

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }


                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });


    }


    /**
     * // 点击加号 判断是否有规格
     *
     * @param id
     */
    private void isAttr(String id, String count, String prePrice, String price, String commodityName, String condimentsId, String cartId, String countcart) {
        EasyHttp.post(new ApplicationLifecycle())
                .api(new SetByCommodityIdApi().setId(id)).request(new OnHttpListener<SetByCommodityIdBean>() {

            @Override
            public void onSucceed(SetByCommodityIdBean data) {
                if (data.getCode() == 0) {
                    if (data.getList().getSpecificationCategoryNameOne() == null) {
// 没有规格 直接加入购物车

                    } else {
                        //        商家小料
//                        shangjiaxiaoliao(data.getList().getSpecificationCategoryNameOne(), data.getList().getSpecificationCategoryListOne(), data.getList().getSpecificationCategoryNameTwo(), data.getList().getSpecificationCategoryListTwo(), id, commodityName, count);
// 有规格 有规格显示规格dialog

                    }

                    shangjiaxiaoliao(data.getList().getSpecificationCategoryNameOne(), data.getList().getSpecificationCategoryListOne(), data.getList().getSpecificationCategoryNameTwo(), data.getList().getSpecificationCategoryListTwo(), id, commodityName, count, price, condimentsId, cartId, countcart);
                }
            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }


    private void addShopCart(String id, String count, String specificationId, String price, String condimentsId) {


        EasyHttp.post(this)
                .api(new AddCartApi().setUserId(SPUtil.get(this, IntentKey.USERID, "")).setCount(count).setCommodityId(id).setMerchantId(merchantid).setSpecificationId(specificationId).setPrice(price).setCondimentsId(condimentsId)).request(new OnHttpListener<PublicMsgBean>() {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    selectMyCart();
                } else {
                    toast(data.getList());
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void selectMyCart() {
        EasyHttp.post(this)
                .api(new SelectMyCartApi().setUserid(SPUtil.get(this, IntentKey.USERID, "")).setPageNum("1").setPageSize("99").setMerchantid(merchantid).setSchoolid(SPUtil.get(this, IntentKey.ADDRESSID, ""))).request(new OnHttpListener<SelectMyCartBean>() {

            @Override
            public void onSucceed(SelectMyCartBean cartdata) {
                if (cartdata.getCode() == 0) {
                    spcunchu(cartdata, cartdata.getList().size());
                    if (cartdata.getList().size() > 0) {

                        tvXiadan.setVisibility(View.VISIBLE);
                        String totalNum = "0";
                        String totalPrice = "0";
                        for (int i = 0; i < cartdata.getList().get(0).getCarts().size(); i++) {
                            if (cartdata.getList().get(0).getCarts().get(i).getGgzzt() == null) {
                                cartdata.getList().get(0).getCarts().get(i).setGgzzt(0);
                            }
                            if (cartdata.getList().get(0).getCarts().get(i).getSpzt() == 0 && cartdata.getList().get(0).getCarts().get(i).getGgzzt() == 0) {

                                totalNum = BigDecimalUtils.add(totalNum, cartdata.getList().get(0).getCarts().get(i).getCount() + "", 0);

                                if (cartdata.getList().get(0).getCarts().get(i).getGgzjg() != null) {
                                    totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getGgzjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                } else {
                                    if (cartdata.getList().get(0).getCarts().get(i).getPrice() != null) {
                                        totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getPrice() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                    } else {

                                        if (cartdata.getList().get(0).getCarts().get(i).getSpjg() != null) {
                                            totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getSpjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                        } else {
                                            totalPrice = BigDecimalUtils.add(totalPrice, BigDecimalUtils.mul(BigDecimalUtils.div(cartdata.getList().get(0).getCarts().get(i).getSpyjg() + "", "100", 2), cartdata.getList().get(0).getCarts().get(i).getCount() + "", 2), 2);
                                        }
                                    }
                                }
                            } else {
                                deleteByIds(cartdata.getList().get(0).getCarts().get(i).getCartId() + "");
                            }
                        }
                        tvMoney.setText(totalPrice);
                        tvNum.setText("已选" + totalNum + "件");
                        dataBeanList.clear();
                        if (cartdata.getList().size() > 0) {
                            dataBeanList.addAll(cartdata.getList().get(0).getCarts());
                        }
                        if (shoppingCartDialog != null) {
                            shoppingCartDialog.setDataList(dataBeanList);
                        }
                    } else {

                        tvXiadan.setVisibility(View.INVISIBLE);
                        tvMoney.setText("0.00");
                        tvNum.setText("已选" + "0" + "件");
                        dataBeanList.clear();
                        if (shoppingCartDialog != null) {
                            shoppingCartDialog.setDataList(dataBeanList);
                        }
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void shareSelectByType() {
        EasyHttp.post(this)
                .api(new ShareSelectByTypeApi().setType("商家").setId(merchantid)).request(new HttpCallback<ShareSelectByTypeBean>(this) {

            @Override
            public void onSucceed(ShareSelectByTypeBean data) {
                if (data.getCode() == 0) {
                    title = data.getList().getTitle();
                    description = data.getList().getContent();
                    logo = data.getList().getImg();
                    if (data.getList().getUrl().contains("http")) {
                        url = data.getList().getUrl();
                    } else {
                        url = "https://www.baidu.com";
                    }

                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void spcunchu(SelectMyCartBean cartdata, int size) {
        if (size > 0) {
            for (int i = 0; i < data.getMerchantInfo().getClassifies().size(); i++) {
                if (data.getMerchantInfo().getClassifies().get(i).getChildrens() != null && data.getMerchantInfo().getClassifies().get(i).getChildrens().size() > 0) {
                    for (int j = 0; j < data.getMerchantInfo().getClassifies().get(i).getChildrens().size(); j++) {
                        if (data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities() != null) {
                            for (int k = 0; k < data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().size(); k++) {


                                data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().get(k).getCart().clear();


                                for (int l = 0; l < cartdata.getList().size(); l++) {
                                    for (int m = 0; m < cartdata.getList().get(l).getCarts().size(); m++) {

//
                                        if (data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().get(k).getCommodityId().equals(cartdata.getList().get(l).getCarts().get(m).getCommodityId())) {


                                            SelectMerchantInfonNewBean.MerchantInfoBean.ClassifiesBean.ChildrensBean.CommoditiesBean.CartBean cartbean = new SelectMerchantInfonNewBean.MerchantInfoBean.ClassifiesBean.ChildrensBean.CommoditiesBean.CartBean();

                                            cartbean.setCartId(cartdata.getList().get(l).getCarts().get(m).getCartId());
                                            cartbean.setCount(cartdata.getList().get(l).getCarts().get(m).getCount());
                                            cartbean.setSpecificationId(cartdata.getList().get(l).getCarts().get(m).getSpecificationId() + "");
                                            cartbean.setCondimentsId(cartdata.getList().get(l).getCarts().get(m).getCondimentsId());


                                            data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().get(k).getCart().add(cartbean);
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }

        } else {

            for (int i = 0; i < data.getMerchantInfo().getClassifies().size(); i++) {
                if (data.getMerchantInfo().getClassifies().get(i).getChildrens() != null && data.getMerchantInfo().getClassifies().get(i).getChildrens().size() > 0) {
                    for (int j = 0; j < data.getMerchantInfo().getClassifies().get(i).getChildrens().size(); j++) {
                        if (data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities() != null) {
                            for (int k = 0; k < data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().size(); k++) {


                                data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().get(k).getCart().clear();


//


                                SelectMerchantInfonNewBean.MerchantInfoBean.ClassifiesBean.ChildrensBean.CommoditiesBean.CartBean cartbean = new SelectMerchantInfonNewBean.MerchantInfoBean.ClassifiesBean.ChildrensBean.CommoditiesBean.CartBean();

                                cartbean.setCartId(0);
                                cartbean.setCount(0);
                                data.getMerchantInfo().getClassifies().get(i).getChildrens().get(j).getCommodities().get(k).getCart().add(cartbean);


                            }
                        }
                    }
                }
            }
        }


        JSONObject jo = null;
        try {
            jo = new JSONObject(com.alibaba.fastjson.JSONObject.toJSONString(data.getMerchantInfo()));
            initColumnData(jo);
            adapter.setData(initDataa(jo), merchantid, tvShopName.getText().toString(), null, null);
            columnAdapter.setData(initColumnData(jo));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 优惠券
     *
     * @param
     * @param
     */
    private void coupon(String name) {
        EasyHttp.post(new ApplicationLifecycle())
                .api(new SelectCOunpByCanteenIdNewApi().setId(merchantid).setPageNum("1").setPageSize("999").setType("0").setUserId(SPUtil.get(this, IntentKey.USERID, ""))).request(new HttpCallback<SelectCOunpByCanteenIdNewBean>(this) {

            @Override
            public void onSucceed(SelectCOunpByCanteenIdNewBean data) {
                if (data.getCoupons().size() > 0) {
                    collectCouponsDialog = new CollectCouponsNewDialog(getContext(), data.getCoupons(), name, merchantid);
                    collectCouponsDialog.show();
                }
            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }

    /*
    分类右面数据
     */
    private JSONArray initDataa(JSONObject body) {

        JSONArray list = body.optJSONArray("classifies");
        JSONArray data = new JSONArray();
        for (int i = 0; i < list.length(); i++) {
            JSONObject listJo = list.optJSONObject(i);
            JSONObject typeList = new JSONObject();
            JSONArray goods = listJo.optJSONArray("childrens");
            try {

                typeList.putOpt("_isGroup", true);
                typeList.putOpt("classifyName", listJo.optString("classifyName") + " ");
                data.put(typeList);
                for (int j = 0; j < goods.length(); j++) {

                    JSONObject childrensJo = goods.optJSONObject(j);
                    JSONArray commoditiesJsa = childrensJo.optJSONArray("commodities");
                    if (commoditiesJsa != null) {
                        for (int z = 0; z < commoditiesJsa.length(); z++) {
                            data.put(commoditiesJsa.optJSONObject(z));

                        }
                    }


                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    /*
     分类左面数据
      */
    private JSONArray initColumnData(JSONObject body) {


        JSONArray list = body.optJSONArray("classifies");
        JSONArray data = new JSONArray();
        for (int i = 0; i < list.length(); i++) {
            JSONObject listJo = list.optJSONObject(i);
            JSONObject typeList = new JSONObject();
            try {
                typeList.putOpt("_isGroup", true);
                typeList.putOpt("classifyName", listJo.optString("classifyName") + " ");
                data.put(typeList);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    private boolean useLoop(List<SelectMyCartBean.ListBean.CartsBean> dataBeanList, String id) {
        for (int i = 0; i < dataBeanList.size(); i++) {
            if (String.valueOf(dataBeanList.get(i).getCommodityId()).equals(id)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_diancan:
                rlDiancan.setVisibility(View.VISIBLE);
                rlPingjia.setVisibility(View.GONE);
                rlShangjia.setVisibility(View.GONE);
                break;
            case R.id.rb_pingjia:
                rlDiancan.setVisibility(View.GONE);
                rlPingjia.setVisibility(View.VISIBLE);
                rlShangjia.setVisibility(View.GONE);
                break;
            case R.id.rb_shangjia:
                rlDiancan.setVisibility(View.GONE);
                rlPingjia.setVisibility(View.GONE);
                rlShangjia.setVisibility(View.VISIBLE);

                break;


        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// 先隐藏键盘

            ((InputMethodManager) etSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(
                            BusinessActivity.this
                                    .getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);


            startActivity(new Intent(this, BusinessSearchActivity.class).putExtra("merchantid", merchantid).putExtra("shopname", shopname).putExtra("searchname", etSearch.getText().toString().trim()));

// 将查询的数据插入数据库


            return true;
        }
        return false;
    }

    @Override
    protected void receiveEvent(Event event) {
        super.receiveEvent(event);
        if (event.getCode() == EvenConstant.PAYSTAUTS) {
//
            finish();
        }
    }


}
